//// 柱状图1模块
(function () {
    var select=0;
    //var dataset1=null;
    var myChart = echarts.init(document.querySelector(".bar .chart"));
    var option;

    option = {
        dataset:[],
        grid: {
            left: "2%",
            containLabel: true
        },
        xAxis: { name: 'amount',
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#65B581',
                    fontSize: 12,
                    fontWeight: 'bold'
                }
            }
        },
        yAxis: { type: 'category' ,
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#65B581',
                    fontSize: 12,
                    fontWeight: 'bold'
                }
            }},
        visualMap: {
            orient: 'horizontal',
            left: 'center',
            min: 0,
            max: 25,
            text: ['High Score', 'Low Score'],
            textStyle: {
                color: '#65B581',
                fontSize: 12,
                fontWeight: 'bold'
            },
            // Map the score column to color
            dimension: 0,
            inRange: {
                color: ['#65B581', '#FFCE34', '#FD665F']
            }
        },
        series: [
            {
                type: 'bar',
                encode: {
                    // Map the "amount" column to X axis.
                    x: 'amount',
                    // Map the "product" column to Y axis
                    y: 'product'
                }
            }
        ]
    };(function () {
        var myChart = echarts.init(document.querySelector(".bar .chart"));
        var option;
        option = {
            dataset:[],
            grid: {
                left: "2%",
                containLabel: true
            },
            xAxis: { name: 'amount',
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#65B581',
                        fontSize: 12,
                        fontWeight: 'bold'
                    }
                }
            },
            yAxis: { type: 'category' ,
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#65B581',
                        fontSize: 12,
                        fontWeight: 'bold'
                    }

                }
                },
            visualMap: {
                orient: 'horizontal',
                left: 'center',
                min: 0,
                max: 40,
                text: ['High Score', 'Low Score'],
                textStyle: {
                    color: '#65B581',
                    fontSize: 12,
                    fontWeight: 'bold'
                },
                // Map the score column to color
                dimension: 0,
                inRange: {
                    color: ['#65B581', '#FFCE34', '#FD665F']
                }
            },
            series: [
                {
                    type: 'bar',
                    encode: {
                        // Map the "amount" column to X axis.
                        x: 'amount',
                        // Map the "product" column to Y axis
                        y: 'product'
                    }
                }
            ]
        };

        function selet1() {
            var sum = 0;
            dataset1 = {source: [['scores', 'amount', 'product']]};
            $.ajax({
                type: "post",
                async: false,
                url: "../connect/Publisher_Rankings.php",
                data: {},
                dataType: "json",
                success: function (result) {
                    if (result) {
                        for (var i = 0; i < result.length; i++) {
                            sum = Number(sum) + Number(result[i].value);
                        }
                        for (i = 0; i < result.length; i++) {
                            dataset1.source.push([Number(result[i].value) * 100 / Number(sum), Number(result[i].value), result[i].name])
                        }
                    }
                },
                error: function (errmsg) {
                    alert("Ajax获取服务器数据出错了！" + JSON.stringify(errmsg));
                }
            });
            return dataset1;
        }
        function selet2() {
            sum = 0;
            dataset2 = {source: [['scores', 'amount', 'product']]};
            //var dataset3=[];
            $.ajax({
                type: "post",
                async: false,
                url: "../connect/Book_Rankings.php",
                data: {},
                dataType: "json",
                success: function (result) {
                    if (result) {
                        for (var i = 0; i < result.length; i++) {
                            sum = Number(sum) + Number(result[i].value);
                        }
                        for (i = 0; i < result.length; i++) {
                            dataset2.source.push([Number(result[i].value) * 100 / Number(sum), Number(result[i].value), result[i].name.slice(0, 3)])
                        }

                    }
                },
                error: function (errmsg) {
                    alert("Ajax获取服务器数据出错了！" + JSON.stringify(errmsg));
                }
            });
            return dataset2;
        }

        var t1 = null;
        t1 = setTimeout(total, 1000); //開始运行
        var dataset1=null;
        var dataset2=null;
        function total() {
            clearTimeout(t1); //清除定时器
            if(select==0){
                dataset1=selet1();
                //alert(dataset1);
                option.dataset = dataset1;
                myChart.setOption(option);
                //setInterval(selet1, 1000);
            }
            else{
                dataset2=selet2();
                option.dataset = dataset2;
                myChart.setOption(option);
                //setInterval(selet2, 1000);
            }
            //total1,total2,total3=TestAjax();
            t1 = setTimeout(total, 1000); //设定定时器，循环运行
            return dataset1,dataset2;
        }
        //alert(0);
        dataset1,dataset2=total()
         //数据变化
        var dataAll = [
            { year: "出版社", data: dataset1 },
            { year: "图书", data: dataset2 }
        ];

        $(".bar h2 ").on("click", "a", function() {
            select=$(this).index();
            option.dataset = dataAll[$(this).index()].data;
            myChart.setOption(option);
        });
    })();
    //option && myChart.setOption(option);
})();

// 柱状图模块
(function() {
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(document.querySelector(".bar1 .chart"));
  var num1=0;
  var num2=0;
    var option;
    option = {
        series: [
            {
                name:"RATE",
                type: "gauge",
                center: ["50%", "60%"],
                radius: "93%", //仪表大小
                // startAngle: 200, //开始角度
                // endAngle: -20, //结束角度
                min:0,
                max:200,
                data:[{value:0,name:"RATE"}],
                axisLine: {
                    show: false,
                    lineStyle: { // 属性lineStyle控制线条样式
                        color: [
                            [0.5, '#FF6F90'],
                            [0.8, '#FFB36F'],
                            [1, '#27D9C8'],
                        ],
                        shadowColor:"#ccc",
                        shadowBlur:25,
                        width: 20
                    }
                },
                splitLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    show: false
                },
                pointer: { //指针样式
                    length: '60%',
                    color:"#4A90E2"
                },
                title:{
                    show:true,
                    offsetCenter: [0, '60%'],
                    color:'#333',
                    fontSize:12,
                    backgroundColor:"#D8d8d8",
                    borderRadius:21,
                    padding:5
                },
                detail: {
                    formatter : "{score|{value}}",
                    offsetCenter: [0, 0],
                    backgroundColor: '#FFF',
                    shadowColor:"#C3Cfff",
                    shadowBlur:14,
                    borderRadius:40,
                    width:38,
                    rich : {
                        score : {
                            color : "#333",
                            fontSize : 17,
                            padding:14
                        }
                    }
                }
            }

        ]
    };

    var t2 = null;
    t2 = setTimeout(total2, 1000); //開始运行
    function total2() {
        clearTimeout(t2); //清除定时器
        $.ajax({
            type: "post",
            async: false,
            url: "../connect/SaleNum.php",
            data: {},
            dataType: "json",
            success: function (result) {
                if (result) {
                    num1=num2;
                    num2=result[0].value;
                }
            },
            error: function(errmsg) {
                alert("Ajax获取服务器数据出错了！"+ JSON.stringify(errmsg));
            }
        })
        var datal=[];
        datal.push({value:num2-num1,name:"RATE"});
        option.series[0].data=datal;
        //alert(data);
        myChart.setOption(option);
        window.addEventListener("resize", function() {
            myChart.resize();
        });
        t2 = setTimeout(total2, 1000); //设定定时器，循环运行
    //    return num1,num2;
    }
    //num1,num2=total2();
})();

(function() {
  // 1. 实例化对象
  var myChart = echarts.init(document.querySelector(".pie1  .chart"));
  // 2. 指定配置项和数据

  var option = {
    legend: {
      top: "90%",
      itemWidth: 10,
      itemHeight: 10,
      textStyle: {
        color: "rgba(255,255,255,.5)",
        fontSize: "12"
      }
    },
    tooltip: {
      trigger: "item",
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    // 注意颜色写的位置
    color: [
      "#006cff",
      "#60cda0",
      "#ed8884",
      "#ff9f7f",
      "#0096ff",
      "#9fe6b8",
      "#32c5e9",
      "#1d9dff"
    ],
    series: [
      {
        name: "点位统计",
        type: "pie",
        // 如果radius是百分比则必须加引号
        radius: ["10%", "70%"],
        center: ["50%", "42%"],
        roseType: "radius",
          //data:dataset,
        data: [
          { value: 0, name: "海南" },
          { value: 0, name: "北京" },
          { value: 0, name: "山东" },
          { value: 0, name: "河北" },
          { value: 0, name: "江苏" },
          { value: 0, name: "浙江" },
          { value: 0, name: "深圳" },
          { value: 0, name: "广东" }
        ],
        // 修饰饼形图文字相关的样式 label对象
        label: {
          fontSize: 10
        },
        // 修饰引导线样式
        labelLine: {
          // 连接到图形的线长度
          length: 10,
          // 连接到文字的线长度
          length2: 10
        }
      }
    ]
  };

    var t2 = null;
    t2 = setTimeout(total2, 1000); //開始运行
    var dataset=[];
    function total2() {
        clearTimeout(t2); //清除定时器
        $.ajax({
            type: "post",
            async: false,
            url: "../connect/prorank.php",
            data: {},
            dataType: "json",
            success: function (result) {
                if (result) {
                    dataset=[];
                    for(var i=0;i<result.length;i++){
                        dataset.push({"value":result[i].value,"name":result[i].name});
                    }
                }
            },
            error: function(errmsg) {
                alert("Ajax获取服务器数据出错了！"+ JSON.stringify(errmsg));
            }
        })
        option.series[0].data = dataset;
        //alert(data);
        myChart.setOption(option);
        window.addEventListener("resize", function() {
            myChart.resize();
        });
        t2 = setTimeout(total2, 1000); //设定定时器，循环运行
        //    return num1,num2;
    }
})();
